<?php

/*
 * @author Ebrain Cedeno <ebraincedeno@gmail.com>
 */

class TPagination extends TWidget{
    
    protected $limit;
    protected $offset;
    protected $total;
    
    public function __construct($parameters) {
        parent::__construct(__CLASS__);
        
        if (!is_array($parameters)){
            throw new Exception('Se esperaba un array. (ejemplo array("limit"=>10,"offset"=>0,"total"=>20); )');
        }
        else{
            if (!array_key_exists("offset", $parameters) AND !array_key_exists("OFFSET", $parameters)){
                throw new Exception("no se encontro el indice offset en el array de parametros");
            }
            else{
                $this->offset = (isset($parameters['offset']))?$parameters['offset']:$parameters['OFFSET'];
            }
            if (!array_key_exists("limit", $parameters) AND !array_key_exists("LIMIT", $parameters)){
                throw new Exception("no se encontro el indice limit en el array de parametros");
            }
            else{
                $this->limit = (isset($parameters['limit']))?$parameters['limit']:$parameters['LIMIT'];
            }
            if (!array_key_exists("total", $parameters) AND !array_key_exists("TOTAL", $parameters)){
                throw new Exception("no se encontro el indice limit en el array de parametros");
            }
            else{
                $this->total = (isset($parameters['total']))?$parameters['total']:$parameters['TOTAL'];
            }
        }
        
    }
    
    public function renderPagination(){
        
        if ($this->Visible == true){
            
            $total_page = ceil($this->total / $this->limit);

            $html_combo = '<select name="'.$this->Name.'" id="'.$this->Id.'">';
            
            for ($count=0;$count < $total_page;$count++){
                
                if ($this->offset == ($this->limit * $count)){
                    $selected = 'selected';
                }
                else{
                    $selected = '';
                }
                    
                $html_combo .= '<option value="'.($count * $this->limit).'" '.$selected.'>'.($count+1).'</option>';
            }
            
            $html_combo .= '</select>';
            
            if ($this->offset > 0){
                $html_left = '<a href="'.$_SERVER['PHP_SELF'].'?offset='.($this->offset-$this->limit).'"><</a>&nbsp;';
            }
            else{
                $html_left ="\n";
            }
            if ($this->offset < ($this->total-1)){
                $html_right = '&nbsp;<a href="'.$_SERVER['PHP_SELF'].'?offset='.($this->offset+$this->limit).'">></a>'."\n";
            }else{
                $html_right = "\n";
            }
            
            
            $javascript ='
                <script type="text/javascript">
                    $(document).ready(function(){
                        $("#'.$this->Id.'").change(function(event){
                            var value_select = $("#'.$this->Id.'").get(0).value;
                            $(location).attr(\'href\',\''.$_SERVER['PHP_SELF'].'?offset=\' + value_select);
                        });
                    });
                </script>

            ';
            
            if ($this->total > $this->limit){
                return $html_left.$html_combo.$html_right.$javascript;
            }
            else{
                return '';
            }
            
            
        }   
    }
}
?>